<!DOCTYPE html>
<html>
<head>
    <title>Touch Adjustment : Adjust context-menu to text-node subtargets - bug 94449</title>
    <script src="../resources/ahem.js"></script>
    <script src="../resources/js-test.js"></script>
    <script src="resources/touchadjustment.js"></script>
    <style>
        #sandbox {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 400px;
            height: 200px;
        }
        #sandbox div {
            padding: 10px;
            font: 20px Ahem;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>

<div id=sandbox>
<!-- The layout of the two words should be: (10,10)x(80,20) and (170,10)x(80,20). -->
    <div id=div1><span id=span1>Text    text</span></div>
</div>

<p id='description'></p>
<div id='console'></div>

<script>
    // Set up shortcut access to elements
    var e = {};
    ['sandbox', 'div1', 'span1'].forEach(function(a) {
        e[a] = document.getElementById(a);
    });

    function testAdjustedTouches()
    {
        var adjustedPoint;
        var word1Bounds = { left: 10, top: 10, width: 80, height: 20 };
        var word2Bounds = { left: 170, top: 10, width: 80, height: 20 };

        // Set editing-behaviour mac, so context-menu gesture triggers selections.
        internals.settings.setEditingBehavior('mac');
        // Check the context-menu is adjusted to the individual words.
        adjustedPoint = adjustTouchPointContextMenu(touchPoint(5, 5, 20, 10));
        shouldBeWithin(adjustedPoint, word1Bounds);
        adjustedPoint = adjustTouchPointContextMenu(touchPoint(100, 15, 20, 10));
        shouldBeWithin(adjustedPoint, word1Bounds);

        adjustedPoint = adjustTouchPointContextMenu(touchPoint(150, 15, 30, 10));
        shouldBeWithin(adjustedPoint, word2Bounds);
        adjustedPoint = adjustTouchPointContextMenu(touchPoint(260, 35, 20, 10));
        shouldBeWithin(adjustedPoint, word2Bounds);

        // Set editing-behaviour to win, so context-menu gesture does not trigger selections.
        internals.settings.setEditingBehavior('win');
        var range = document.createRange();
        range.setStart(e.span1.firstChild, 2);
        range.setEnd(e.span1.firstChild, 6);
        window.getSelection().addRange(range);
        var selectionBounds = { left: 50, top: 10, width: 80, height: 20 };
        adjustedPoint = adjustTouchPointContextMenu(touchPoint(25, 5, 30, 20));
        shouldBeWithin(adjustedPoint, selectionBounds);
        adjustedPoint = adjustTouchPointContextMenu(touchPoint(140, 10, 20, 10));
        shouldBeWithin(adjustedPoint, selectionBounds);
    }

    function runTests()
    {
        if (window.testRunner && window.internals && internals.touchNodeAdjustedToBestContextMenuNode) {
            description('Test touch-adjustment to individual words or active selection.');
            testAdjustedTouches();
            e.sandbox.style.display = 'none';
        }
    }
    runTests();
</script>


</body>
</html>
